package com.tiantian.quanminxiu.dialog;

import android.app.Activity;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.presenter.DialogOpenGuradPresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.DialogBase;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.DialogOpenGuradView;

import org.json.JSONArray;
import org.json.JSONObject;

import rx.functions.Action1;

/**
 * Created by admin on 2016/9/14.
 */
public class Dialog_OpenGuard extends DialogBase implements DialogOpenGuradView{

    private Activity activity;
    private String UserId;
    private String NickName;
    private int monthPrice;
    private int monthId;
    private int yearPrice;
    private int yearID;
    private ImageView imgDialogIcon;
    private TextView txtMonthGuard,txtYearGuard,txtGuardPrice,txtGuardName;
    private View txtCancel,txtSub;

    private int Type = monthId;

    private DialogOpenGuradPresent present;

    public Dialog_OpenGuard(Activity activity, int themeResId, String UserId, String NickName) {
        super(activity, themeResId);
        this.activity = activity;
        this.UserId = UserId;
        this.NickName = NickName;
        InitView(activity);
    }

    @Override
    protected void InitView(Activity activity) {
        setContentView(R.layout.dialog_openguard);
        Init(activity);

        JSONArray guardConfig = UserData.Current.GuardConfigList;
        if(guardConfig==null&&guardConfig.length()<2){
            CommonUtils.showToask(thisContext, "守护数据出错，请重新登录");
            dismiss();
        }

        present = new DialogOpenGuradPresent();
        present.attachView(this);

        for (int i=0;i<guardConfig.length();i++){
            JSONObject object = guardConfig.optJSONObject(i);
            if("MONTH".equalsIgnoreCase(object.optString("Mode"))){
                monthPrice = object.optInt("Price");
                monthId = object.optInt("id");
            }else if("YEAR".equalsIgnoreCase(object.optString("Mode"))){
                yearPrice = object.optInt("Price");
                yearID = object.optInt("id");
            }
        }

        imgDialogIcon = (ImageView) findViewById(R.id.imgDialogIcon);
        txtCancel = findViewById(R.id.txtCancel);
        txtSub = findViewById(R.id.txtSub);
        txtMonthGuard = (TextView) findViewById(R.id.txtMonthGuard);
        txtYearGuard = (TextView) findViewById(R.id.txtYearGuard);
        txtGuardName = (TextView) findViewById(R.id.txtGuardName);
        txtGuardPrice = (TextView) findViewById(R.id.txtGuardPrice);
        imgDialogIcon.post(new Runnable() {
            @Override
            public void run() {
                APPUtils.displayImage("drawable://" + R.drawable.dialog_icon_openguard, imgDialogIcon, thisContext, imgDialogIcon.getWidth() / 2);
            }
        });
        txtGuardName.setText(Html.fromHtml("守护对象: " + "<font color='#ffde3b'>" + NickName + "</font>"));
        txtGuardPrice.setText(Html.fromHtml("守护费用: " + "<font color='#ffde3b'>" + monthPrice + "/月</font>"));
        Type = monthId;

        RxView.clicks(txtSub).map(View->txtSub).subscribe(OC_Button);
        RxView.clicks(txtCancel).map(View->txtCancel).subscribe(OC_Button);
        RxView.clicks(txtYearGuard).map(View->txtYearGuard).subscribe(OC_Button);
        RxView.clicks(txtMonthGuard).map(View->txtMonthGuard).subscribe(OC_Button);
    }

    public Action1<View> OC_Button = v->{
        switch (v.getId()){
            case R.id.txtSub:
                present.openGuard(Type+"");
                break;
            case R.id.txtCancel:
                dismiss();
                break;
            case R.id.txtYearGuard:
                txtYearGuard.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.wihte));
                txtYearGuard.setBackground(APPUtils.getResourcesDrawble(thisContext, R.drawable.shape_radius_yellow));
                txtMonthGuard.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.yellow));
                txtMonthGuard.setBackground(APPUtils.getResourcesDrawble(thisContext, R.drawable.shape_border_yellow));
                txtGuardPrice.setText(Html.fromHtml("守护费用: " + "<font color='#ffde3b'>" + yearPrice + "/年</font>"));
                Type = yearID;
                break;
            case R.id.txtMonthGuard:
                txtYearGuard.setTextColor(APPUtils.getResourcesColor(thisContext,R.color.yellow));
                txtYearGuard.setBackground(APPUtils.getResourcesDrawble(thisContext, R.drawable.shape_border_yellow));
                txtMonthGuard.setTextColor(APPUtils.getResourcesColor(thisContext, R.color.wihte));
                txtMonthGuard.setBackground(APPUtils.getResourcesDrawble(thisContext, R.drawable.shape_radius_yellow));
                txtGuardPrice.setText(Html.fromHtml("守护费用: " + "<font color='#ffde3b'>" + monthPrice + "/月</font>"));
                Type = monthId;
                break;
        }
    };

    @Override
    public void dismiss() {
        super.dismiss();
        present.detachView();
    }

    @Override
    public void openGurad() {
        dismiss();
    }

}
